import { defineStore } from 'pinia'
import { getPublishGoods } from '@/service/module/user/user'
interface GoodsType {
 goods: any
  page: number
  pageSize: number
  hasMore: boolean
  loading: boolean
  searchParams: any
}
export const useUserPublishStore = defineStore('userPublishStore', {
  state: (): GoodsType => ({
    goods: [],
    page: 1,
    pageSize: 20,
    hasMore: true,
    loading: false,
    searchParams: {},
  }),
  // actions: {
  //   // 获取发布商品列表
  //   async getPublishGoodsList() {
  //     const res = await getPublishGoods()
  //     this.goods = res.data.records
  //   },
  // },
  actions: {
    // 初始化或刷新
    async initGoodsList(params = {}) {
      this.page = 1
      this.hasMore = true
      this.goods = []
      await this.loadMoreGoods({ pageSize: this.pageSize, ...params })
    },
    // 加载更多
    async loadMoreGoods(params = {}) {
      if (this.loading || !this.hasMore) return
      this.loading = true
      const res = await getPublishGoods({ pageNo: this.page, pageSize: this.pageSize, ...params })
      const records = res.data.records || []
      if (this.page === 1) {
        this.goods = records
      } else {
        this.goods = [...this.goods, ...records]
      }
      this.hasMore = records.length === this.pageSize
      this.page += 1
      this.loading = false
    },
  },
})
